#!/usr/local/bin/ruby
=begin

Copyright 2010 Arachne Jericho <arachne.jericho@gmail.com>

This file is part of RubyTea.

RubyTea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RubyTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RubyTea.  If not, see <http://www.gnu.org/licenses/>.

=end
#
# Lists one's collection of tea in a nice way. 
#

require 'ruby-tea.rb'
require 'cgi'

TEA_FIELD_ORDER = ['Type', 'Character']

header('My Tea Collection')
puts <<END
<p>Ratings explanation:</p>
<ul>
<li><strong>5:</strong> I would <em>marry</em> this tea.</li>
<li><strong>4:</strong> I'm <em>good friends</em> with this tea.</li>
<li><strong>3:</strong> I'm <em>aquainted</em> with this tea.</li>
<li><strong>2:</strong> I have a <em>personality clash</em> with this tea.</li>
<li><strong>1:</strong> I <em>never want</em> this tea to darken my doorstep again.</li>
<li><strong>0:</strong> <em>Nuke this tea from orbit.</em> It's the only way to make sure.</li>
<li>No rating: not enough sippings to rate this tea.</li>
</ul>
<p>Ratings may change as time passes.  Also, these are personal ratings; your mileage may vary.</p>
<p>Other notes:</p>
<ul>
<li>Empty sippings field means no sippings.</li>
<li>[search] will search for this tea name on my website (I may not have talked about it yet).</li>
<li>The name of the tea will, if available, take you to the blender's website or somewhere else you can purchase it.</li>
<li>Some ratings will have a review, if I've gotten around to it.</li>
<li>Faded teas means that I've let it go from my collection.</li>
</ul>
<p><strong>Blenders:</strong></p>
END

puts '<ul>'
$tea_collection.keys.sort.each do |blender_name|
    puts %Q(<li><a href="##{anchorize blender_name}">#{blender_name}</a></li>)
end
puts '</ul>'

$tea_collection.keys.sort.each do |blender_name|

    puts %Q(<h2 id="#{anchorize blender_name}">#{CGI::escapeHTML blender_name}</h2>)
    puts '<table><tr><th>Name</th>'
    TEA_FIELD_ORDER.each { |field| puts "<th>#{field}</th>" }
    puts '<th>Rating/Sips</th></tr>'

    blender = $tea_collection[blender_name]


    blender.keys.sort.each do |tea_name|

        tea = blender[tea_name]

        tr_attrs = tea.has_key?('Gone') ? %Q( class="gone") : %Q( class="#{CGI::escapeHTML tea['Type']}")

        tea_link_attrs = tea.has_key?('Last Sipped') ? %Q( title="Last sipped #{tea['Last Sipped']}") : ''
        tea_link = tea.has_key?('URL') ? %Q( <a href="#{tea['URL']}"#{tea_link_attrs} target="_top">#{CGI::escapeHTML tea_name}</a>) : " <span#{tea_link_attrs}>#{CGI::escapeHTML tea_name}</span>"

        search_link = %Q(<a class="search_td" title="Search S∂" href='http://tea.spontaneousderivation.com/?s="#{CGI::escape(tea_name)}"#{blender_name == 'Anonymous' ? '' : %Q(+"#{CGI::escape(blender_name)}")}' target="_top">[search]</a>)

        sippings_text = ''
        if tea.has_key? ('Sippings')
            num_sips = tea['Sippings'].to_i
            case num_sips
            when 0
                # nop
            when 1
                sippings_text = '(1 sip)'
            else
                sippings_text = "(#{num_sips} sips)"
            end
        end
        
        review_link = tea.has_key?('Review') ? %Q( <strong><a href="#{tea['Review']}" title="Review" target="_top">#{tea['Rating']}<sup>*</sup></a></strong>) : " #{tea['Rating']}"

        puts %Q(<tr#{tr_attrs}><td>#{tea_link}<br />#{search_link}</td>)

        TEA_FIELD_ORDER.each do |field|
            puts "<td>#{tea[field]}</td>"
        end

        puts %Q(<td>#{review_link} #{sippings_text}</td>)
        puts '</tr>'
    end

    puts "</table>"
end

footer()
